package de.rj.urwalking.ui.fragments.dialog;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.threed.jpct.Object3D;

import de.rj.urwalking.R;
import de.rj.urwalking.data.Pin;
import de.rj.urwalking.db.DBSalientObject;
import de.rj.urwalking.ui.activities.QuestionsActivity;

/**
 * 
 * @author rjj
 *
 */
public class NewLandmarkDialogFragment extends LandmarkDialogFragment implements OnClickListener {

	private boolean removePin = true;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = super.onCreateView(inflater, container, savedInstanceState);

		getDialog().setTitle(getResources().getString(R.string.new_landmark));
		
		hideInfoView();

		return view;
	}

	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.suggestButton:

			// prevent pin from being removed
			removePin = false;

			// make sure there's an experiment running
			ensureExperiment();

			Pin pin = (Pin) getArguments().getSerializable("object");

			// show the Activity with the questions
			Intent intent = new Intent(getActivity(), QuestionsActivity.class);
			intent.putExtra("object", new DBSalientObject(null, null, null, null, pin.getRealX(), pin.getRealY(), null));
			startQuestions(intent);

			this.dismiss();
			break;
		case R.id.backButton:
			this.dismiss();
			break;
		default:
			break;
		}

	}

	@Override
	public void onDismiss(DialogInterface dialog) {
		super.onDismiss(dialog);

		if (removePin) {
			// remove the pin for the new object if the user changes his mind and cancels the dialog
			Object3D object = (Object3D) getArguments().getSerializable("object");
			object.clearObject();
		}
	}

}
